package com.xf.web.controller.sys;

import java.util.ArrayList;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.xf.pm.common.PageData;
import com.xf.pm.common.PageForm;
import com.xf.pm.util.ResultMsg;
import com.xf.sys.entity.SysMenu;
import com.xf.sys.entity.TreeNode;
import com.xf.sys.service.ISysMenuService;
import com.xf.web.util.SessionUtil;

/**
 * xf-web
 * SysMenuController.java
 * author:lb
 * 2017年7月3日
 */
@Controller
@RequestMapping("/sysMenu")
public class SysMenuController {
	
	private final Logger logger =  LoggerFactory.getLogger(this.getClass());
	
	@Autowired
	private ISysMenuService sysMenuService;
	
	/**
	 * 初始化界面
	 * @return
	 */
	@RequestMapping(value = "/list")
	public String list()
	{
		return "sys/menu/list";
	}
	
	/**
	 * 菜单列表查询
	 * @param form
	 * @param sysMenu
	 * @return
	 */
	@RequestMapping(value = "/selectSysMenuList")
	public @ResponseBody PageData selectSysMenuList(PageForm form,SysMenu sysMenu)
	{
		return sysMenuService.selectSysMenuList(form,sysMenu);
	}
	
	/**
	 * 新增菜单信息
	 * @param sysMenu
	 * @return
	 */
	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public @ResponseBody ResultMsg addSysMenu(SysMenu sysMenu){
		ResultMsg msg=new ResultMsg();
        try {
        	if(null!=sysMenu){
        		int result=sysMenuService.insertSysMenu(sysMenu);
        		if(result==1){
        			msg.setMsg("添加菜单成功");
            		msg.setSuccess(true);
        		}else{
        			msg.setMsg("添加菜单失败");
            		msg.setSuccess(false);
        		}
            }
		} catch (Exception e) {
			logger.error("添加菜单结束异常");
		    e.printStackTrace();
		}
        return msg;
	}
	
	/**
	 * 删除菜单信息
	 * @param ids
	 * @return
	 */
	@RequestMapping(value = "/remove", method = RequestMethod.POST)
	public @ResponseBody ResultMsg removeSysMenu(Integer[] ids){
		ResultMsg msg=new ResultMsg();
        try {
        	if(null!=ids&&ids.length>0){
        		int result=sysMenuService.deleteSysMenu(ids);
        		if(result>0){
        			msg.setMsg("删除菜单成功");
            		msg.setSuccess(true);
        		}else{
        			msg.setMsg("删除菜单失败");
            		msg.setSuccess(false);
        		}
            }
		} catch (Exception e) {
			logger.error("删除菜单结束异常");
		    e.printStackTrace();
		}
		return msg;
	}
	
	/**
	 * 修改菜单信息
	 * @param sysMenu
	 * @return
	 */
	@RequestMapping(value = "/update", method = RequestMethod.POST)
	public @ResponseBody ResultMsg updateSysMenu(SysMenu sysMenu){
		ResultMsg msg=new ResultMsg();
        try {
        	if(null!=sysMenu){
        		int result=sysMenuService.updateSysMenu(sysMenu);
        		if(result==1){
        			msg.setMsg("修改菜单成功");
            		msg.setSuccess(true);
        		}else{
        			msg.setMsg("修改菜单失败");
            		msg.setSuccess(false);
        		}
            }
		} catch (Exception e) {
			logger.error("修改菜单结束异常");
		    e.printStackTrace();
		}
        return msg;
	}
	
	/**
	 * 查询单个菜单
	 * @param menuId
	 * @param model
	 * @return
	 */
	@RequestMapping(value = "/edit")
	public String edit(Integer menuId, ModelMap model)
	{
		if (menuId != null)
		{
			model.addAttribute("dto", sysMenuService.selectSysMenu(menuId));
		}
		return "sys/menu/edit";
	}
	
	/**
	 * 查询菜单tree
	 * @return
	 */
	@RequestMapping(value = "/selectParentMenu")
	public @ResponseBody List<TreeNode> selectParentMenu(){
		List<TreeNode> list=new ArrayList<TreeNode>();
		try {
			list=sysMenuService.listTree(SessionUtil.getSession().getRoleId());
		} catch (Exception e) {
			logger.error("查询一级菜单异常");
			e.printStackTrace();
		}
		return list;
	}

}
